/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.ui

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.view.animation.AccelerateInterpolator
import android.widget.Scroller
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.climber.android.R
import com.climber.android.commonsdk.app.AppConstants
import com.climber.android.commonres.ui.BaseMVPActivity
import com.climber.android.commonsdk.arouter.ARouterPathConstants
import com.climber.android.commonsdk.helper.newFragment
import io.ganguo.library.mvp.Config
import io.ganguo.library.mvp.ui.mvp.IPresenter
import io.ganguo.library.mvp.ui.mvp.di.component.AppComponent
import kotlinx.android.synthetic.main.app_activity_welcome.*


/**
 * Created by mikes on 2017-4-11.
 */
fun ViewPager.setViewPagerScroller(){
    try {
        val scrollerField = ViewPager::class.java.getDeclaredField("mScroller")
        scrollerField.isAccessible = true
        val interpolator = ViewPager::class.java.getDeclaredField("sInterpolator")
        interpolator.isAccessible = true

        val scroller = object : Scroller(this.context, AccelerateInterpolator()) {
            override fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int, duration: Int) {
                super.startScroll(startX, startY, dx, dy, 300)
            }
        }
        scrollerField.set(this, scroller)
    } catch (e: NoSuchFieldException) {
        // Do nothing.
    } catch (e: IllegalAccessException) {
        // Do nothing.
    }
}

@Route(path = ARouterPathConstants.PATH_APP_WELCOME)
class WelcomeActivity : BaseMVPActivity<IPresenter>() {
    private val defaultPNGs = mapOf<Int, Int>(
        0 to R.drawable.app_welcome_1,
        1 to R.drawable.app_welcome_2
    )

    private val dynamicPNGs : List<String> by lazy {
        (intent.getStringExtra("novicePNGs") ?: "").split(",").filter { it.isNotEmpty() }
    }

    override fun changeStatusColor(): Boolean = false

    override fun setupActivityComponent(appComponent: AppComponent) {
    }

    override fun getLayoutResourceId(): Int = R.layout.app_activity_welcome

    override fun initView() {
        vp_welcome.offscreenPageLimit = if(dynamicPNGs.isNotEmpty()){
            dynamicPNGs.size - 1
        } else{
            defaultPNGs.size - 1
        }
        vp_welcome.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return if(dynamicPNGs.isNotEmpty()){
                    this@WelcomeActivity.newFragment<WelcomeFragment>(Pair("welcomePNG", dynamicPNGs[position]))
                } else{
                    this@WelcomeActivity.newFragment<WelcomeFragment>(Pair("defaultPNG", defaultPNGs[position]!!))
                }
            }

            override fun getCount(): Int{
                return if(dynamicPNGs.isNotEmpty()){
                    dynamicPNGs.size
                } else{
                    defaultPNGs.size
                }
            }
        }

        vp_welcome.setViewPagerScroller()
    }

    override fun initListener() {
    }

    override fun initData() {
        Config.putBoolean(AppConstants.CONFIG_LAUNCH_AFTER_DOWNLOAD, false)
    }

    fun skipWelcome(view: View){
        onBackPressed()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        fullP()
        super.onCreate(savedInstanceState)

        hideNavigationBar()
    }

    private fun hideNavigationBar() {
        //SYSTEM_UI_FLAG_FULLSCREEN 全屏时，状态栏被隐藏
        //SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 全屏时，布局不会占用状态栏
        //SYSTEM_UI_FLAG_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE 状态栏隐藏，再出现时，不会重新计算布局
        //SYSTEM_UI_FLAG_HIDE_NAVIGATION 导航栏不显示，布局延伸到导航栏
        //SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 导航栏显示，布局延伸到导航栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val decorView = window.decorView
            val option =
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.navigationBarColor = Color.TRANSPARENT
        } else {
            val decorView = window.decorView
            val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
            decorView.systemUiVisibility = uiOptions
        }
    }

    /**
     * 9.0全屏,兼容水滴屏
     *
     */
    private fun fullP() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val lp = window.attributes
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            window.attributes = lp
        }
    }

    override fun onBackPressed() {
        setResult(Activity.RESULT_OK)
        finish()
    }
}